﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Cors;

namespace Curry.Forum.Api
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            //允许跨域
            //config.EnableCors(new EnableCorsAttribute("*", "*", "*"));

            //根据配置文件设置跨域
            var headers=  ConfigurationManager.AppSettings["cors:ForumHeaders"];
            var methods = ConfigurationManager.AppSettings["cors:ForumMethods"];
            var origins = ConfigurationManager.AppSettings["cors:ForumOrigins"];
            var supportCors = new EnableCorsAttribute(origins, headers, methods)
            {
                SupportsCredentials = true//资源是否支持请求中的用户凭据
            };
            config.EnableCors(supportCors);

            // Web API 配置和服务

            // Web API 路由
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
